package org.lanettiesso.dubbo.simple.beans.factory.annotation;

import lombok.extern.slf4j.Slf4j;
import org.lanettiesso.dubbo.simple.annotation.Service;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.core.type.filter.AnnotationTypeFilter;

import java.util.Set;

@Slf4j
public class ServiceClassPostProcessor implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        String basePackage = "org.lanettiesso.dubbo.simple";
        SimpleDubboClassPathBeanDefinitionScanner scanner = new SimpleDubboClassPathBeanDefinitionScanner(registry);
        scanner.addIncludeFilter(new AnnotationTypeFilter(Service.class));
        scanner.scan(basePackage);
        Set<BeanDefinitionHolder> beanDefinitionHolders = scanner.getBeanDefinitionHolders();
        System.out.println(basePackage);
        for (BeanDefinitionHolder definitionHolder : beanDefinitionHolders) {
            registry.registerBeanDefinition(definitionHolder.getBeanName(), definitionHolder.getBeanDefinition());
            log.info("registry bean: " + definitionHolder.getBeanName());
        }
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }
}
